import requests
from json import dumps

# Define URL for Crossref API with required parameters
api_url = 'https://api.crossref.org/works'
query_params = {
    'mailto': "934096384@qq.com",
    'filter': 'is-referencedbys',  # filter by references
    'rows': 10,                  # max rows of results to return
    'sort': 'bestmatch',
    'type': 'journal-article',   # type of work: journal articles, conference objects etc.
}

title = "Chronic kidney disease in the developing world"
author = "Barsoum RS"

query_params.update({
    'title': title,
    'author': {
        'family': author.split(' ')[1],
        'given': author.split(' ')[0]
    },
})

# Make GET request to Crossref API
response = requests.get(api_url, params=query_params)

# Check the response status code for success (204 is no content)
if response.status_code == 204:
  print("No DOI found")
else:
  # Parse JSON data from response body
  data = response.json()

  # Assuming there should be only one result, we get it and return its DOI field.
  if 'message' in data:
    first_result = data['message'][0]
    doi = first_result.get('DOI', None)
    print(dumps({'doi': doi}, indent=2))
  else:
    print("No DOI found")

